/**
 *
 */
package et.oschina.common.search.test;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import net.oschina.common.search.IndexHolder;
import net.oschina.common.search.SearchHelper;
import net.oschina.common.search.Searchable;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.core.util.UuidUtil;
import org.apache.lucene.search.Query;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.zs.pig.storm.LogBean;

/**
 * 测试索引过程
 *
 * @author Winter Lau
 */
public class LuceneTester {


    Post post1 = new Post(1, "post title1", "post body1");
    Post post2 = new Post(2, "阿里云携手开源中国众包平台发布百万悬赏项目", "阿里云与开源中国达成战略合作，首期将通过开源中国众包平台发布近百万元悬赏项目");
    Post post3 = new Post(3, "SQLite 3.9.0 发布，数据库服务器", "SQLite是遵守ACID的关联式数据库管理系统，它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。");

    Person person1 = new Person(1, "红薯", "深圳市");
    Person person2 = new Person(2, "马云", "杭州市");
    Person person3 = new Person(3, "蜘蛛侠", "US");
    IndexHolder holder;

    @Before
    public void setUp() throws Exception {
        String indexFolder = LuceneTester.class.getClassLoader().getResource("./").getFile() + "index";

     //   FileUtils.deleteDirectory(new File(indexFolder));
        FileUtils.forceMkdir(new File(indexFolder));
        holder = IndexHolder.init("C:\\data\\index1");
    }
    @Test
    public void testIndex1() throws Exception {
    	Long a=System.currentTimeMillis();
    	SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
    	
    	for(int i=0;i<10000;i++){
    		String uid1=UuidUtil.getTimeBasedUuid().toString();
    		String uid2=UuidUtil.getTimeBasedUuid().toString();
    		String uid3="26af2593-fd5c-11e6-afc2-204747756de8";
    	
    		
    		String mesg=""+uid1+" - "+uid3+" - 3 - INFO - 2017:10:11 09:57:41.254 - 12-4B-00-0D-BB-64-72/10.2.10.28 - android:1029/default - DeviceController/report/start - {\"sn\":\"042h-dfEv\",\"order\":1,\"apiKey\":\"d3ec610c*3160*49f9*9ef9*1f7f87fdca6c\",\"deviceSn\":\"00-12-4B-00-0D-BB-64-72\"}";
    		  
    		LogBean	logBean=new LogBean(idWorker.nextId(),mesg.split(" - ")[0],mesg.split(" - ")[1],mesg.split(" - ")[2],mesg.split(" - ")[3],
                    mesg.split(" - ")[4],mesg.split(" - ")[5],mesg.split(" - ")[6],mesg.split(" - ")[7],mesg.split(" - ")[8]);
        	holder.add(Arrays.asList(logBean));
    	}
    	Long b=System.currentTimeMillis();
    	System.out.println(b-a);
    }
    @Test
    public void testIndex() throws Exception {
        holder.add(Arrays.asList(post1, post2, post3));
        for (int i = 0; i < 10; i++)
            holder.optimize(Post.class);

//        Query query = SearchHelper.makeQuery("title", "数据库", 10.f);
//        List<Long> ids = holder.find(Post.class, query, null, null, 1, 10);
//
//        System.out.println(ids.size());
//System.out.println(ids.get(0).longValue());
//        holder.delete(Arrays.asList(post3));
//        for (int i = 0; i < 10; i++)
//            holder.optimize(Post.class);
//
//        List<Long> afterDeleted = holder.find(Post.class, query, null, null, 1, 10);
//        System.out.println(afterDeleted.size());
//        Assert.assertEquals(0, afterDeleted.size());
    }

    @Test
    public void testMultiIndex() throws Exception {
        holder.add(Arrays.asList(post1, post2, post3));
        holder.add(Arrays.asList(person1, person2, person3));
        Query query = SearchHelper.makeQuery("title", "蜘蛛侠", 10.f);
        List<Searchable> results = holder.find(Arrays.asList(Post.class, Person.class), query, null, null, 1, 10);
        Assert.assertEquals(1, results.size());
        Assert.assertEquals(3l, results.get(0).id());

        person3.setTitle("数据库");
        holder.update(Arrays.asList(person3));
        for (int i = 0; i < 10; i ++){
            holder.optimize(Person.class);
        }

        List<Searchable> results1 = holder.find(Arrays.asList(Post.class, Person.class),
                SearchHelper.makeQuery("title", "数据库", 10.f), null, null, 1, 10);
        Assert.assertEquals(2, results1.size());

        Assert.assertEquals(2, holder.count(Arrays.asList(Post.class, Person.class), SearchHelper.makeQuery("title", "数据库", 10.f), null));
        for (int i = 0; i < 10; i ++){
            holder.optimize(Person.class);
        }

        Assert.assertEquals(0, holder.count(Arrays.asList(Post.class, Person.class), SearchHelper.makeQuery("title", "蜘蛛侠", 10.f), null));
    }
}
